using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;

namespace TST.SharePoint.Shared
{
    public class WSSFieldHyperlink : FieldHyperlink
    {
        private SPFieldUrl _field;

        /// <summary>
        /// Constructor.
        /// </summary>
        public WSSFieldHyperlink()
        {
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="ID">The ID for the field.</param>
        public WSSFieldHyperlink(string Id, bool renderAsImage)
            : base(Id, renderAsImage)
        {
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        public WSSFieldHyperlink(SPFieldUrl field)
        {
            if (field != null)
            {
                this.Id = field.Id.ToString();
                _field = field;
                base.RenderAsImage = (field.DisplayFormat == SPUrlFieldFormatType.Image);
            }
        }

        private string GetTitle(string hyperlink)
        {
            if (hyperlink.IndexOf(",") > 0)
            {
                return hyperlink.Substring(hyperlink.IndexOf(",") + 1);
            }
            return hyperlink;
        }

        public override string GetTypedValueAsString(object value)
        {
            if (value == null)
                return null;
            return GetTitle(value.ToString());
        }

        public override string GetTypedValueFormatted(object value)
        {
            if (value == null)
                return null;
            return GetTitle(value.ToString());
        }

    }
}
